import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		titOn: 'all',
		pageIndex: 1,
		pageItems: 10,
		totalPages: 0,
		studyStatus: '',
		studyList: []
	},
	handleTap1(event) {   //任务筛选
		var sessType = event.target.id;
		var going;
		var pass;
		var abence;
		if (sessType == "going") {
			going = "goingShow";
			pass = "passHide";
			abence = "abenceHide";
		} else if (sessType == "pass") {
			pass = "passShow";
			going = "goingHide";
			abence = "abenceHide";
		} else if (sessType == "abence") {
			abence = "abenceShow";
			going = "goingHide";
			pass = "passHide";
		} else if (sessType == "all") {
			abence = "abenceShow";
			going = "goingShow";
			pass = "passShow";
		}
		this.setData({
			titOn: sessType,
			going: going,
			pass: pass,
			abence: abence
		});
		var status = event.currentTarget.dataset.status; 
		this.setData({
			"status": status,
			"studyList": [],
			"pageIndex": 1,
			"totalPages": 1
		});
		//第四个未完成标签状态值为4,表示查询进行中和未开始的
		this.loadTaskList();
	},
	articleDetail(event) {  //学习
		var taskId = event.currentTarget.dataset.taskid;
		var name = event.currentTarget.dataset.name;
 		var pageUrl = "../missionDetail/missionDetail?fromPage=task&taskId=" + taskId + '&name=' + name + '&courseId=';
		wx.navigateTo({
			url: pageUrl
		})
	},
	onLoad(params) {
		this.setData({
			"status": 0
		});
		this.loadTaskList();
	},
	loadTaskList() {
		//加载任务列表
		var newUrl = getApp().globalData.dangjianUrl + "/study/studyList";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["status"] = this.data.status + "";
		params["studyStatus"] = "";
 		params = JSON.stringify(params);
		console.log("学习任务列表请求参数：" + params);
		this.httpGet(newUrl, params, "查询任务列表接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var studys = respData.data.studys;
					var studyList = [];
					if (null != studys && undefined != studys) {
						var totalPages = studys.totalPages;
						var pageIndex = studys.pageIndex;
						studys = studys.study;
						for (var index in studys) {
							var record = studys[index];
							var status = record.status;
							var statusName = "";
							if (3 == status) {
								statusName = "已完成";
							} else if (2 == status) {
								statusName = "进行中";
							} else {
								statusName = "未开始";
							}
							record.statusName = statusName;
							var url = "";
							var imageItems = record.imageItems;
							if (null != imageItems && undefined != imageItems) {
								imageItems = imageItems.imageFile;
								url = imageItems[0].url;
							}
							record.url = url;
							studyList.push(record);
						}
						var oldStudyList = this.data.studyList;
						studyList = oldStudyList.concat(studyList);
						this.setData({
							"studyList": studyList,
							"pageIndex": pageIndex, 
							"totalPages": totalPages
						});
					}
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},
	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex
			});
			this.loadTaskList();
		} else {
			this.showPageMsg("tips", "没有更多内容了!");
		}
	},
	onPullDownRefresh: function() {
		this.setData({
			pageIndex: 1,
			studyList: []
		});
		this.loadTaskList();
		wx.stopPullDownRefresh();
	}
});
